#include "water_temperature_curve.h"
#include "ui_water_temperature_curve.h"
#include<QDebug>
Water_Temperature_Curve::Water_Temperature_Curve(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Water_Temperature_Curve)
{
    ui->setupUi(this);
    chart = new QChart;
    lineSeries=new QSplineSeries;
    axisX=new QDateTimeAxis;
    axisY=new QValueAxis;
    chart_init();
}

Water_Temperature_Curve::~Water_Temperature_Curve()
{
    delete axisX;
    delete axisY;
    delete lineSeries;
    delete chart;
    delete ui;
}

void Water_Temperature_Curve::chart_init()
{
    //设置曲线的名称
    lineSeries->setName("实时水温曲线图");
    //把曲线添加到图表中
    chart->addSeries(lineSeries);
    //设置X坐标轴
    axisX->setFormat("hh:mm:ss");
    axisX->setTickCount(10);
    axisX->setTitleText("日期时间");
    //设置Y坐标轴
    axisY->setRange(0,100);
    axisY->setTickCount(10);
    axisY->setTitleText("温度");
    //设置网格不显示
    axisY->setGridLineVisible(false);
    //将坐标轴添加到图表中
    chart->addAxis(axisX,Qt::AlignBottom);
    chart->addAxis(axisY,Qt::AlignLeft);
    //把曲线关联到坐标轴
    lineSeries->attachAxis(axisX);
    lineSeries->attachAxis(axisY);
    //把chart图表显示到窗口上
    ui->graphicsView->setChart(chart);
    //设置渲染：抗锯齿
    ui->graphicsView->setRenderHint(QPainter::Antialiasing);
}

void Water_Temperature_Curve::update_data(QDateTime currentTime,int temperature)
{
    qDebug()<<currentTime<<"and"<<temperature;
    lineSeries->append(currentTime.toMSecsSinceEpoch(),temperature);
    axisX->setMin(currentTime.addSecs(-1*10));
    axisX->setMax(currentTime.addSecs(1*10));
}

//void Water_Temperature_Curve::test()
//{
//    lineSeries->append(QDateTime::currentDateTime().toMSecsSinceEpoch(),10);
//    lineSeries->append(QDateTime::currentDateTime().addSecs(10).toMSecsSinceEpoch(),20);
//    lineSeries->append(QDateTime::currentDateTime().addSecs(20).toMSecsSinceEpoch(),30);
//    lineSeries->append(QDateTime::currentDateTime().addSecs(30).toMSecsSinceEpoch(),40);
//    axisX->setMin(QDateTime::currentDateTime().addSecs(-1*10));
//    axisX->setMax(QDateTime::currentDateTime().addSecs(1*10));
//}



